package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WorkerMapper;
import com.ruoyi.system.domain.Worker;
import com.ruoyi.system.service.IWorkerService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class WorkerServiceImpl implements IWorkerService
{
    @Autowired
    private WorkerMapper workerMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Worker selectWorkerById(Long id)
    {
        return workerMapper.selectWorkerById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param worker 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Worker> selectWorkerList(Worker worker)
    {
        return workerMapper.selectWorkerList(worker);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param worker 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertWorker(Worker worker)
    {
        return workerMapper.insertWorker(worker);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param worker 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateWorker(Worker worker)
    {
        return workerMapper.updateWorker(worker);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteWorkerByIds(Long[] ids)
    {
        return workerMapper.deleteWorkerByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteWorkerById(Long id)
    {
        return workerMapper.deleteWorkerById(id);
    }

    @Override
    public String importWorkers(List<Worker> workerList, boolean updateSupport) {
        if (StringUtils.isNull(workerList) || workerList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Worker worker:workerList)
        {
            try
            {

                this.insertWorker(worker);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、员工名称 ").append(worker.getName()).append(" 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工名称 " + worker.getName() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
